package com.winter.portal.server.controller;

import com.winter.portal.api.dto.ResourceDTO;
import com.winter.portal.api.vo.LeftMenuVO;
import com.winter.portal.api.vo.ResourceVO;
import com.winter.portal.server.constant.Constants;
import com.winter.portal.server.entity.ResourceEntity;
import com.winter.portal.server.service.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jzyan.framework.core.response.CommonRes;
import org.jzyan.framework.core.utils.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.groups.Default;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 资源
 * </p>
 *
 * @author jzyan
 * @since 2023-01-30
 */
@RestController
@RequestMapping(Constants.ADMIN_PREFIX + "/resource")
@Api(value = "资源接口", tags = "资源管理")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @GetMapping("/authorized/left/menu")
    @ApiOperation(value = "左侧菜单", tags = "资源管理")
    public CommonRes<LeftMenuVO> menuTree() {
        LeftMenuVO leftMenuVO = resourceService.menuTree();
        return CommonRes.success(leftMenuVO);
    }

    @GetMapping("table")
    @ApiOperation(value = "资源列表", tags = "资源管理")
    public CommonRes<List<ResourceVO>> table() {
        return CommonRes.success(resourceService.table());
    }

    @GetMapping
    @ApiOperation(value = "资源详情", tags = "资源管理")
    public CommonRes<ResourceVO> detail(@RequestParam Serializable id) {
        ResourceEntity resourceEntity = resourceService.getById(id);
        return CommonRes.success(ConvertUtil.convert(resourceEntity, ResourceVO.class));
    }

    @PostMapping
    @ApiOperation(value = "添加资源", tags = "资源管理")
    public CommonRes add(@RequestBody @Valid ResourceDTO resource) {
        resourceService.add(resource);
        return CommonRes.success();
    }

    @PutMapping
    @ApiOperation(value = "更新资源", tags = "资源管理")
    public CommonRes update(@RequestBody @Validated(value = {Default.class, ResourceDTO.Update.class}) ResourceDTO resource) {
        resourceService.update(resource);
        return CommonRes.success();
    }

    @DeleteMapping
    @ApiOperation(value = "删除资源", tags = "资源管理")
    public CommonRes delete(@RequestParam Long id) {
        resourceService.delete(id);
        return CommonRes.success();
    }

}
